package com.jnxx.okr.controller;

import com.jnxx.basic.query.PageInfo;
import com.jnxx.basic.util.AjaxResult;
import com.jnxx.okr.domain.Renew;
import com.jnxx.okr.query.RenewQuery;
import com.jnxx.okr.service.IRenewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* 后端接口类；保存okr的评论以及更新内容
*/
@RestController
@RequestMapping("/api/renew")
public class RenewController {

    @Autowired
    public IRenewService renewService;

    /**
     * 接口：添加或修改
     * @param renewMap  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Map<String, Renew> renewMap){
        Renew renew = renewMap.get("renew");
        if( renew.getId()!=null){
		    renewService.update(renew);
        }else{
            renewService.add(renew);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        renewService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        renewService.batchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Renew findOne(@PathVariable("id")Long id) {
        return renewService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Renew> findAll(){
        return renewService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageInfo<Renew> page(@RequestBody RenewQuery query) {
        return renewService.page(query);
    }

    @GetMapping("/getRenewListByOkrId/{okrId}")
    public List<Renew> getRenewListByOkrId(@PathVariable("okrId")Long okrId) {
        return renewService.getRenewListByOkrId(okrId);
    }
}
